using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Windows;
using System.Windows.Ink;
using WozLib.UI.Ink;
using WozLib.UI.Ink.SketchObjects;

namespace WozLib.WozObjects
{
  public class SketchPadServerObject : ServerObject
  {

    [XmlAttribute("Description")]
    public string Description
    {
      get
      {
        if (ServerObjectRegistry.ContainsObject(this.ServerID))
        {
          IServerObject iServerObject = ServerObjectRegistry.GetObject(this.ServerID);
          if (iServerObject is SketchPad)
          {
            return (iServerObject as SketchPad).Description;
          }
          else
          {
            // this shouldn't happen
            return null;
          }
        }
        else
        {
          return _Description;
        }
      }

      set { _Description = value; }
    } string _Description;

    [XmlAttribute("Width")]
    public double Width
    {
      get
      {
        if (ServerObjectRegistry.ContainsObject(this.ServerID))
        {
          IServerObject iServerObject = ServerObjectRegistry.GetObject(this.ServerID);
          if (iServerObject is SketchPad)
          {
            return (iServerObject as SketchPad).Width;
          }
          else
          {
            // this shouldn't happen
            return 0.0;
          }
        }
        else
        {
          return _Width;
        }
      }

      set { _Width = value; }
    } double _Width;

    [XmlAttribute("Height")]
    public double Height
    {
      get
      {
        if (ServerObjectRegistry.ContainsObject(this.ServerID))
        {
          IServerObject iServerObject = ServerObjectRegistry.GetObject(this.ServerID);
          if (iServerObject is SketchPad)
          {
            return (iServerObject as SketchPad).Height;
          }
          else
          {
            // this shouldn't happen
            return 0.0;
          }
        }
        else
        {
          return _Height;
        }
      }

      set { _Height = value; }
    } double _Height;

    [XmlAttribute("UserTool")]
    public SketchPad.InkTools UserTool
    {
      get
      {
        if (ServerObjectRegistry.ContainsObject(this.ServerID))
        {
          IServerObject iServerObject = ServerObjectRegistry.GetObject(this.ServerID);
          if (iServerObject is SketchPad)
          {
            return (iServerObject as SketchPad).UserCurrentInkTool;
          }
          else
          {
            // this shouldn't happen
            return SketchPad.InkTools.None;
          }
        }
        else
        {
          return _UserTool;
        }
      }

      set { _UserTool = value; }
    } SketchPad.InkTools _UserTool;

    [XmlElement("UserTools")]
    public List<SketchPad.InkTools> UserTools
    {
      get
      {
        if (ServerObjectRegistry.ContainsObject(this.ServerID))
        {
          IServerObject iServerObject = ServerObjectRegistry.GetObject(this.ServerID);
          if (iServerObject is SketchPad)
          {
            return (iServerObject as SketchPad).UserToolsEnabledCollection;

          }
          else
          {
            // this shouldn't happen
            return new List<SketchPad.InkTools>();
          }
        }
        else
        {
          return _UserTools;
        }
      }

      set
      {
        // won't ever get called
        // the serializer does a get, then adds to that collection
        _UserTools = value;
      }
    }
    [XmlIgnore]
    public List<SketchPad.InkTools> _UserTools = new List<SketchPad.InkTools>();

    [XmlElement("WozObjects")]
    public List<SketchObject> WozObjects
    {
      get
      {
        if (ServerObjectRegistry.ContainsObject(this.ServerID))
        {
          IServerObject iServerObject = ServerObjectRegistry.GetObject(this.ServerID);
          if (iServerObject is SketchPad)
          {
            List<SketchObject> wozObjects = new List<SketchObject>();

            // get each woz object currently in the underlying canvas and push it into
            // our list of woz objects

            foreach (UIElement uie in (iServerObject as SketchPad).Children)
            {
              if (uie is ISketchObject)
              {
                wozObjects.Add((uie as ISketchObject).GetWozObject());
              }
            }

            foreach (Stroke stroke in (iServerObject as SketchPad).Strokes)
            {
              if (stroke is ISketchObject)
              {
                wozObjects.Add((stroke as ISketchObject).GetWozObject());
              }
            }

            if (wozObjects.Count == 0)
            {
              return _WozObjects;
            }
            else
            {
              return wozObjects;
            }

          }
          else
          {
            // this shouldn't happen
            return new List<SketchObject>();
          }
        }
        else
        {
          return _WozObjects;
        }
      }

      set 
      { 
        // won't ever get called
        // the serializer does a get, then adds to that collection
        _WozObjects = value; 
      }
    } 
    [XmlIgnore]
    public List<SketchObject> _WozObjects = new List<SketchObject>();

    public SketchPadServerObject()
    {
    }

    public SketchPadServerObject(long serverID) : base (serverID)
    {
    }

    public override IServerObject GetIServerObject()
    {
      // we'll first try to let our base pull it out of the registry or create one 
      IServerObject iServerObject = base.GetIServerObject();

      // if our base didn't couldn't find the IServerObject
      // or if it's of the wrong type, we'll try to make our own
      if (iServerObject == null || !(iServerObject is SketchPad))
      {
        iServerObject = SketchPad.FromSketchPadServerObject(this);

        // if we do not the IWozObject we just created is not registered, register it
        if (iServerObject != null && !ServerObjectRegistry.ContainsObject(iServerObject.ServerID))
        {
          ServerObjectRegistry.RegisterObjectWithExisitingId(iServerObject);
        }
      }

      return iServerObject;
    }
  }
}
